package com.xxc.service.impl;

/**
 * @author xiangcheng
 * @version 2022/6/22 11:37
 * @since JDK8
 */

import com.xxc.dao.StudentDao;
import com.xxc.dao.StudentTagDao;
import com.xxc.dto.StudentRequestDTO;
import com.xxc.dto.StudentResponseDTO;
import com.xxc.entity.Student;
import com.xxc.entity.StudentTag;
import com.xxc.feign.CityClient;
import com.xxc.feign.ClazzClient;
import com.xxc.feign.GroupClient;
import com.xxc.feign.TagClient;
import com.xxc.service.StudentService;
import com.xxc.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service("studentService")
public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao;
    private final CityClient cityClient;
    private final GroupClient groupClient;
    private final ClazzClient clazzClient;
    private final StudentTagDao studentTagDao;
    private final TagClient tagClient;

    @Autowired
    public StudentServiceImpl(StudentDao studentDao, CityClient cityClient, GroupClient groupClient, ClazzClient clazzClient, StudentTagDao studentTagDao, TagClient tagClient) {
        this.studentDao = studentDao;
        this.cityClient = cityClient;
        this.groupClient = groupClient;
        this.clazzClient = clazzClient;
        this.studentTagDao = studentTagDao;
        this.tagClient = tagClient;
    }

    @Override
    public List<StudentResponseDTO> queryAll() {
        List<Student> students = studentDao.queryAll();
        List<StudentResponseDTO> dtos = new ArrayList<>();
        students.forEach(student -> {
            StudentResponseDTO dto = new StudentResponseDTO();
            //使用工具类复制属性
            BeanUtils.copyProperties(student,dto);

            dto.setClazz(clazzClient.clazz(student.getClazzId()));
            dto.setCity(cityClient.city(student.getCityId()));
            dto.setGroup(groupClient.group(student.getGroupId()));

            List<Integer> tagIds = studentTagDao.queryByStudentId(student.getId());
            tagIds.forEach(tagId -> {
                dto.getTags().add(tagClient.tag(tagId));
            });

            dtos.add(dto);
        });

        return dtos;
    }

    @Override
    public StudentResponseDTO insert(StudentRequestDTO studentRequestDTO) {
        //1.转为学生对象
        Student student = new Student();
        BeanUtils.copyProperties(studentRequestDTO, student);
        //2.设置学生(年龄 属相 星座)业务属性
        student.setAge(DateUtil.getAge(student.getBir()));//年龄
        student.setAttr(DateUtil.getYear(student.getBir()));//属相
        student.setStarts(DateUtil.getConstellation(student.getBir()));//星座
        //3.保存学生
        studentDao.insert(student);  //student 存在自己id
        List<Integer> tagIds = studentRequestDTO.getTagIds();
        if (!ObjectUtils.isEmpty(tagIds)) {
            //4.保存学生标签关系
            tagIds.forEach(tagId ->
                    studentTagDao.insert(new StudentTag(student.getId(), tagId))
            );
        }
        StudentResponseDTO studentResponseDTO = new StudentResponseDTO();
        BeanUtils.copyProperties(student, studentResponseDTO);
        studentResponseDTO.setClazz(clazzClient.clazz(student.getClazzId()));
        studentResponseDTO.setCity(cityClient.city(student.getCityId()));
        studentResponseDTO.setGroup(groupClient.group(student.getGroupId()));
        //根据学生id获取学生标签id集合
        tagIds.forEach(id -> {
            studentResponseDTO.getTags().add(tagClient.tag(id));
        });

        return studentResponseDTO;
    }
}
